package com.ryan.mdp.client.normal;

import java.util.Date;

import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import com.ryan.mdp.api.normal.DemoMdpService;
import com.ryan.mdp.callback.MdpCallback;
import com.ryan.mdp.dto.DemoDto;

@ContextConfiguration(locations = { "classpath:/context/context-mdp-demo-client.xml" })
public class DemoMdpClient extends AbstractJUnit4SpringContextTests {

    private boolean asyncSuccess = false;

    @Autowired
    private DemoMdpService demoMdpClient;

    @org.junit.Test
    public void testEchoNum() {
        Assert.assertNotNull(demoMdpClient.echo(1));
    }

    @org.junit.Test
    public void testEcho1() {
        demoMdpClient.echo("liaosir");
        System.out.println("testEcho1()");
    }

    @org.junit.Test
    public void testEcho2() {
        setAsyncSuccess(false);
        demoMdpClient.echo("liaosir", new MdpCallback() {
            public void invoke(Object object) {
                System.out.println(object);
                setAsyncSuccess(true);
            }
        });
        try {
            Thread.sleep(500);
            Assert.assertTrue(isAsyncSuccess());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @org.junit.Test
    public void testEcho3() {
        String echoResponse = demoMdpClient.echoResponse("liaosir");
        System.out.println(echoResponse);
    }

    @org.junit.Test(expected = Exception.class)
    public void testEcho4() throws Exception {
        demoMdpClient.echoException("liaosir");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @org.junit.Test
    public void testEchoBynary() {
        byte[] result = demoMdpClient.echoBinary(new byte[] { 1, 2, 3 });
        System.out.println(result);
    }

    @org.junit.Test
    public void testEchoDto() {
        DemoDto dto = new DemoDto();
        dto.setProp1("value");
        dto = demoMdpClient.echoDto(new Date(), dto);
        System.out.println(dto.getProp1());
    }

    @org.junit.Test
    public void testEchoDto2() {
        DemoDto dto = new DemoDto();
        dto.setProp1("value");
        dto = demoMdpClient.echoDto(1, new Date(), dto);
        System.out.println(dto.getProp1());
    }

    public boolean isAsyncSuccess() {
        return asyncSuccess;
    }

    public void setAsyncSuccess(boolean asyncSuccess) {
        this.asyncSuccess = asyncSuccess;
    }
}
